﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;
using System.Linq;
using System.Threading.Tasks;

namespace NeuroProzessorSvc
{
	[RunInstaller(true)]
	public partial class ProjectInstaller : System.Configuration.Install.Installer
	{
		public ProjectInstaller()
		{
			string[] commandlineArgs = Environment.GetCommandLineArgs();

			string servicename;
			string servicedisplayname;
			ParseServiceNameSwitches(commandlineArgs, out servicename, out servicedisplayname);

			InitializeComponent(servicedisplayname, servicename);
		}

		/// <summary>
		/// Args-Parser für den Installer / uninstaller
		/// </summary>
		/// <param name="commandlineArgs"></param>
		/// <param name="serviceName"></param>
		/// <param name="serviceDisplayName"></param>
		private void ParseServiceNameSwitches(string[] commandlineArgs, out string serviceName, out string serviceDisplayName)
		{
			var servicenameswitch = (from s in commandlineArgs where s.StartsWith("/servicename") select s).FirstOrDefault();
			var servicedisplaynameswitch = (from s in commandlineArgs where s.StartsWith("/servicedisplayname") select s).FirstOrDefault();

			if (servicenameswitch == null)
				throw new ArgumentException("Argument 'servicename' fehlt");
			if (servicedisplaynameswitch == null)
				throw new ArgumentException("Argument 'servicedisplayname' fehlt");
			if (!(servicenameswitch.Contains('=') || servicenameswitch.Split('=').Length < 2))
				throw new ArgumentNullException("Der /servicename switch ist fehlerhaft");

			if (!(servicedisplaynameswitch.Contains('=') || servicedisplaynameswitch.Split('=').Length < 2))
				throw new ArgumentNullException("Der /servicedisplaynameswitch switch is fehlerhaft");

			serviceName = servicenameswitch.Split('=')[1];
			serviceDisplayName = servicedisplaynameswitch.Split('=')[1];

			serviceName = serviceName.Trim('"');
			serviceDisplayName= serviceDisplayName.Trim('"');
		}
	}
}
